package com.topisv.tms.web;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.topisv.tms.entity.JSONParam;
import com.topisv.tms.entity.OrdenoSource;
import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.service.OrdenoSourceService;
import com.topisv.tms.web.base.BaseDataTableController;

/**
 * 订单来源配置
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/ordenoSource")
public class OrdenoSourceController extends BaseDataTableController{

	private static Map<String, String> sortTypes = Maps.newLinkedHashMap();
	static {
		sortTypes.put("auto", "自动");
		sortTypes.put("showname", "功能名称");
	}

	@Autowired
	private OrdenoSourceService ordenoSourceService;

	@RequestMapping(value = "v2_list")
	public String list(@RequestParam(value = "sortType", defaultValue = "auto") String sortType,@RequestParam(value = "function", defaultValue = "0") int function,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber, Model model, HttpServletResponse request) {
		return "ordenoSource/ordenoSourceList";
	}
	@RequestMapping(value = "page")
	public void page(@RequestBody JSONParam[] paramJson, HttpServletRequest request,HttpServletResponse response){
		try {
			// 客户端参数
			QueryParamHolder paramHolder = collectParam(paramJson);
			Page<OrdenoSource> page = ordenoSourceService.pageOrdenoSource(paramHolder);
			// 封装查询结果
			response.getWriter().print(bulidJSON(page));
		} catch (Exception e) {
			
		}
	}
	
	@RequestMapping(value = "v2_create", method = RequestMethod.GET)
	public String createOrdenoSource(Model model) {
		model.addAttribute("ordenoSource", new OrdenoSource());
		model.addAttribute("action", "create");
		model.addAttribute("status", "1");
		return "ordenoSource/ordenoSourceForm";
	}
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@Valid OrdenoSource ordenoSource, RedirectAttributes redirectAttributes) {
		try {
			ordenoSource.setModifyDate(new Date());
			String[] values=ordenoSource.getOrdenoSourceCode().split("--");
			ordenoSource.setOrdenoSourceCode(values[0]);
			ordenoSource.setOrdenoSourceName(values[1]);
			ordenoSourceService.save(ordenoSource);
			redirectAttributes.addFlashAttribute("message", "创建功能成功");
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", e.getMessage());
		}
		return "redirect:/ordenoSource/v2_list";
	}
	
	

	@RequestMapping(value = "v2_update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		
		model.addAttribute("ordenoSource",ordenoSourceService.findOne(id));
		model.addAttribute("action", "v2_update");
		return "ordenoSource/ordenoSourceForm";
	}

	@RequestMapping(value = "v2_update", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("preloadOrdenoSource") OrdenoSource ordenoSource, RedirectAttributes redirectAttributes) {
		try {
			OrdenoSource tempOrdenoSource=ordenoSourceService.findOne(ordenoSource.getId());
			ordenoSource.setOrdenoSourceName(tempOrdenoSource.getOrdenoSourceName());
			ordenoSource.setOrdenoSourceCode(tempOrdenoSource.getOrdenoSourceCode());
			ordenoSource.setModifyDate(new Date());
			ordenoSourceService.save(ordenoSource);
			redirectAttributes.addFlashAttribute("message", "更新功能成功");
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", e.getMessage());
		}
		return "redirect:/ordenoSource/v2_list";
	}

}
